﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;

namespace AnyGrid.Metadata
{
    /// <summary>
    /// Adds support for data annotation attributes omitted from DataAnnotationsModelMetadataProvider
    /// </summary>
    public class ExtendedDataAnnotationsModelMetadataProvider : DataAnnotationsModelMetadataProvider
    {
        protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
        {
            var result = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);

            DisplayAttribute da = attributes.OfType<DisplayAttribute>().FirstOrDefault();
            if (da != null)
            {
                var autoGenerate = da.GetAutoGenerateFilter();
                if (autoGenerate.HasValue)
                {
                    result.AdditionalValues[AdditionalValuesKeys.AutoGenerateFilter] = autoGenerate.Value;
                }
                var groupName = da.GroupName;
                if (!string.IsNullOrEmpty(groupName))
                {
                    result.AdditionalValues[AdditionalValuesKeys.GroupName] = groupName;
                }
                if (!string.IsNullOrEmpty(da.Prompt))
                {
                    result.Watermark = da.Prompt;
                }

                // Any Display(Order=)?
                var order = da.GetOrder();
                if (order.HasValue)
                {
                    result.AdditionalValues.Add(AdditionalValuesKeys.DisplayOrder, order);
                }
            }

            DisplayColumnAttribute dc = attributes.OfType<DisplayColumnAttribute>().FirstOrDefault();
            if (dc != null)
            {
                var sc = dc.SortColumn;
                if (!string.IsNullOrEmpty(sc))
                {
                    result.AdditionalValues[AdditionalValuesKeys.SortColumnName] = sc;
                    if (dc.SortDescending)
                    {
                        result.AdditionalValues[AdditionalValuesKeys.SortDescending] = true;
                    }
                }
            }

            GridColumnAttribute gca = attributes.OfType<GridColumnAttribute>().FirstOrDefault();
            if (gca != null)
            {
                if (gca.CustomOptions != null && gca.CustomOptions.Any())
                {
                    var opts = new ColumnOptionCollection(gca.CustomOptions);
                    result.AdditionalValues[AdditionalValuesKeys.GridOptions] = opts;
                }
                if (gca.DisableSort)
                {
                    result.AdditionalValues[AdditionalValuesKeys.DisableSort] = true;
                }
            }

            KeyAttribute ka = attributes.OfType<KeyAttribute>().FirstOrDefault();
            if (ka != null)
            {
                result.AdditionalValues[AdditionalValuesKeys.IsKeyMember] = true;
            }

            StringLengthAttribute sla = attributes.OfType<StringLengthAttribute>().FirstOrDefault();
            if (sla != null)
            {
                result.AdditionalValues[AdditionalValuesKeys.MaximumStringLength] = sla.MaximumLength;
            }

            return result;
        }

        public override IEnumerable<ModelMetadata> GetMetadataForProperties(object container, Type containerType)
        {
            var result = base.GetMetadataForProperties(container, containerType);

            // If we have any display attributes with order property set, then sort by them.  If a property doesn't have one, then default to zero.
            return result.OrderBy(m => m.AdditionalValues.ContainsKey(AdditionalValuesKeys.DisplayOrder) ? m.AdditionalValues[AdditionalValuesKeys.DisplayOrder] : 0 );
        }
    }
}
